# lxml安装失败
from lxml import html
import requests

headers = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36"
}
url = "https://book.douban.com/"
request = requests.get(url, headers=headers)
request.encoding = "utf-8"
text = request.text
etree = html.etree  # 构造出etree对象
selector = etree.HTML(text)
coverImages = selector.xpath("//*[@id='content']/div/div[1]/div[1]/div[2]/div/div/ul[2]/*/div[1]/a/img/@src")
titles = selector.xpath('//*[@id="content"]/div/div[1]/div[1]/div[2]/div/div/ul[2]/*/div[2]/div[1]/a/text()')
authors = selector.xpath('//*[@id="content"]/div/div[1]/div[1]/div[2]/div/div/ul[2]/*/div[2]/div[2]/text()')
abstracts = selector.xpath('//*[@id="content"]/div/div[1]/div[1]/div[2]/div/div/ul[2]/*/div[2]/div[3]/p[2]/text()')
for coverImage, title, author, abstract in zip(coverImages, titles, authors, abstracts):
    data = {
        "coverImage": coverImage.strip(),
        "title": title.strip(),
        "author": author.strip(),
        "abstract": abstract.strip()
    }
    print(data)
